package initialize

import (
	"fmt"
	"go-bluebell/model"

	"go.uber.org/zap"

	"github.com/go-redis/redis"
	"github.com/spf13/viper"
)

func RedisInit() (err error) {
	model.Client = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", viper.GetString("redis.host"), viper.GetInt("redis.port")),
		Password: viper.GetString("password"), // 密码
		DB:       viper.GetInt("db"),          // 数据库
		PoolSize: viper.GetInt("pool_size"),   // 连接池大小
	})

	_, err = model.Client.Ping().Result()
	if err != nil {
		zap.L().Error("Redis连接失败！", zap.Error(err))
	}
	return err
}

func RedisClose() {
	_ = model.Client.Close()
}
